<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>数据模型配置</title>
<{$showjs}>
<link rel="stylesheet" type="text/css" href="/Plug-in/ext/resources/css/xtheme-slate.css" />
<link rel="stylesheet" type="text/css" href="../css/main1.css" />
<style>
.d_new{background-image:url(../img/table_add.png)!important;}
.d_del{background-image:url(../img/table_delete)!important;}
.d_refresh{background-image:url(../img/table_refresh.png)!important;}
.d_save{background-image:url(../img/table_save.png)!important;}
.d_edit{background-image:url(../img/table_edit.png)!important;}
.d_cfg{background-image:url(../img/table_gear.png)!important;}
.d_check{background-image:url(../img/table_sort.png)!important;}
.d_fx{background-image:url(../img/table_relationship.png)!important;}
.d_yh{background-image:url(../img/table_lightning.png)!important;}
.d_xf{background-image:url(../img/table_error.png)!important;}
.i_filed{background-image:url(../img/application_form.png)!important;}

</style>
<script language="javascript">

Ext.onReady(function(){
function refresh(){
                    Ext.getBody().mask('等待[waiting]...','x-mask-loading');
                    store.reload();
                    setTimeout(function(){
					Ext.getBody().unmask();
					},1000);
                    }
function newtable(){
                formPanel.form.reset();
				Ext.getCmp('tabname').enable();
				Ext.getCmp('extend_name').enable();
                	}
function savedata(next){
								formPanel.form.doAction('submit',{
								url:'./tablep.php',
								method:'POST',
								params:{op:'save'},
								waitMsg:'正在提交[Submiting]....',
								success:function(form,action){
											if (action.result.msg=='ok') {
												var E=Ext.Msg.alert('提示[Notice]','操作成功[Operate successfully]')
												store.reload();
												} else {
												Ext.Msg.alert('失败',action.result.msg);
											}
									 },
									 //提交失败的回调函数
									 failure:function(){
											Ext.Msg.alert('错误','数据提交错误！');
									 }
								});
							}
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';
	var proxy = new Ext.data.HttpProxy({
                        url : 'tablep.php'
                    });
	var fields = [		{name : 'id'},
						{name : 'id_mirror'},
						{name : 'name'},
                    	{name : 'tabname'},
                    	{name : 'declare'},
                    	{name : 'extend_id'},
                    	{name : 'extend_name'},
                    	{name : 'shield'},
                    	{name : 'created'}
                        ];
        var reader = new Ext.data.JsonReader(
            		{
                    }, fields);

        var store = new Ext.data.Store({
        				baseParams: {op: 'shownowtable'},
                        proxy : proxy,
                        reader : reader
                    });
		store.load();

		var cols = [
		{ id : 'name', header: "名称", sortable: true, dataIndex: 'name'},
		{ id : 'tabname', header: "表名", sortable: true, dataIndex: 'tabname'},
		{ id : 'created', header: "是否已创建", sortable: true, dataIndex: 'created',renderer:function(val){
				return val==1?'<font color=green><center><b>√</b></center></font>':'<font color=red><center><b>×</b></center></font>';
		}}
	];
	var grid = new Ext.grid.GridPanel({
		ddGroup          : 'gridDDGroup',
        store            : store,
        columns          : cols,
		enableDragDrop   : true,
        stripeRows       : true,
        autoExpandColumn : 'name',
        height			 : 300,
        width            : 350,
		region           : 'north',
        //title          : '已有模型',
		selModel         : new Ext.grid.RowSelectionModel({singleSelect:true})
    });

    var hidden_id = new Ext.form.Hidden({
        name:'id_mirror',
        value:'0'
    })
	var text_name = new Ext.form.TextField({
		width      : 260,
		fieldLabel : '模型名称',
		name       : 'name',
		allowBlank : false,
		blankText:'模型名称不能为空'
	});
	var text_tabname = new Ext.form.TextField({
		id		   : 'tabname',
		width      : 260,
		fieldLabel : '表名',
		name       : 'tabname',
		allowBlank : false,
		blankText  : '表名不能为空'
	});
	var hidden_extend_id = new Ext.form.Hidden({
        name:'extend_id',
        value:'0'
    });
	 var textarea_declare = new Ext.form.TextArea({
	 	name:'declare',
		fieldLabel:'说明',
		width:260
    });
	var combo_extend_name = new Ext.form.ComboBox({
		id : 'extend_name',
		name : 'extend_name',
		emptyText  :'不继承',
		fieldLabel : '所继承的表',
		allowBlank : true,
		width:260,
		store:new Ext.data.SimpleStore({fields:[],data:[[]]}),
		mode:'local',
		tpl: '<div id="tree" style="overflow:auto;"></div>',
        triggerAction:"all"
		});
	var opRoot = new Ext.tree.AsyncTreeNode({
        id:'0',
        text:'根'
    });
    var loader = new Ext.tree.TreeLoader({
        dataUrl:'table.php?op=combo_tree'
    });
    var opTree = new Ext.tree.TreePanel({
        anchor : '95%',
        frame :true,
        enableDD:true,
        animate : false,
        rootVisible :false,
        useArrows:true,
        border:false,
        height:300,
        containerScroll: true,
        autoScroll:true,
        loader:loader,
        root:opRoot  //之前定义的根节点
    });
	//加载之前所触发的事件
    //树节点点击事件
    opTree.on('click',function(node) {
        combo_extend_name.setValue(node.attributes.text);
        hidden_extend_id.setValue(node.attributes.id);
        combo_extend_name.collapse();
        //alert(combo_Hidden.getValue());
    });
    combo_extend_name.on('expand',function(){
        opTree.render('tree');
        opTree.root.reload();
        opTree.root.expand(true,true);
    });

    var	 foot=new Ext.Panel({
		region:'south',
		height:30,
		bbar:['->',{xtype: 'button',iconCls:'d_new',text:'新建表',handler:newtable},'-',{xtype: 'button',iconCls:'d_save',text:'保存',handler:savedata},'-',{xtype: 'button',iconCls:'i_filed',text:'字段配置',handler:function(){
					window.location.href='field.php';
				}}]
		});
	var formPanel = new Ext.form.FormPanel({
		name:'formPanel',
		//buttonAlign:'center',
    	labelAlign:'right',
    	buttonAlign: 'right',
		region     : 'center',
		iconCls:'d_edit',
		title      : '模型详细配置',
		bodyStyle  : 'padding: 10px; background-color: #EEEEEE',
		labelWidth : 80,
		border	:true,
		//width      : 400,
		frame	   : true,
		autoScroll:true,
		items      :
		{
		xtype:'fieldset',
        title: '数据模型-基本信息',
        autoHeight: true,
       	name:'basic',
		items:[
			hidden_id,
			text_name,
			text_tabname,
			hidden_extend_id,
			combo_extend_name,
			textarea_declare
		]}
	});
	var ct = new Ext.Panel({
		title: '已有模型',
		region: 'west',
		iconCls:'d_cfg',
		width: 350,
		//height: 400,
		layout: 'border',
		items: [
			grid,
			{
				id: 'detailPanel',
				region: 'center',
				bodyStyle: {
					background: '#CCCCCC',
					padding: '7px'
				},
				html: '选择一个数据模型…并拖拽到右边'
			}
		]
	})
var bookTplMarkup = [
		'模型名称:{name}<br/>',
		'表名:{tabname}<br/>',
		'说明:{declare}<br/>'
	];
var bookTpl = new Ext.Template(bookTplMarkup);
var viewport = new Ext.Viewport({
			layout:'border',
			items    : [
			ct,
			formPanel,
			foot
		]

		});
grid.getSelectionModel().on('rowselect', function(sm, rowIdx, r) {
		var detailPanel = Ext.getCmp('detailPanel');
		bookTpl.overwrite(detailPanel.body, r.data);
	});
var blankRecord =  Ext.data.Record.create(fields);
	var formPanelDropTargetEl =  formPanel.body.dom;

	var formPanelDropTarget = new Ext.dd.DropTarget(formPanelDropTargetEl, {
		ddGroup     : 'gridDDGroup',
		notifyEnter : function(ddSource, e, data) {
			formPanel.body.stopFx();
			formPanel.body.highlight();
		},
		notifyDrop  : function(ddSource, e, data){
			var selectedRecord = ddSource.dragData.selections[0];
			if(selectedRecord.data.created=='1'){
            	Ext.getCmp('tabname').disable();
            	Ext.getCmp('extend_name').disable();
            }else{
				Ext.getCmp('tabname').enable();
				Ext.getCmp('extend_name').enable();
            }
			formPanel.getForm().loadRecord(selectedRecord);
			return(true);
		}
	});
grid.addListener('rowcontextmenu', rightClickFn);
function rightClickFn(client, rowIndex, e) {
                e.preventDefault();
                var menus = new Ext.menu.Menu
        ([
            {
                xtype:"button",text:"重载表",iconCls:'d_refresh',pressed:true,
                handler:refresh
            },{
                xtype:"button",text:"新建表",iconCls:'d_new',pressed:true,
                handler:newtable
            },
            {
                xtype:"button",text:"删除表",iconCls:'d_del',pressed:true,
                handler:function(){
				var record=store.getAt(rowIndex);
				var currid =record.data.id;
				Ext.Ajax.request({
				url : 'tablep.php',
				params : {op:'del',id:currid},
				success:function(response){
											if (response.responseText=='ok') {
												Ext.getBody().mask('等待...','x-mask-loading');
                    							store.reload();
							                    setTimeout(function(){
												Ext.getBody().unmask();
												},500);
												} else {
												Ext.Msg.alert('数据编辑',response.responseText);
											}
									 },
									 //提交失败的回调函数
									 failure:function(){
											Ext.Msg.alert('错误','数据提交错误！');
									 }
				});
				}
            },
            {
                xtype:"button",text:"检查表",iconCls:'d_check',pressed:true,
                handler:function(){
				var record=store.getAt(rowIndex);
				var currid =record.data.id;
				Ext.Ajax.request({
				url : 'tablep.php',
				params : {op:'check',id:currid},
				success:function(response){
											if (response.responseText=='ok') {
												Ext.getBody().mask('等待...','x-mask-loading');
                    							store.reload();
							                    setTimeout(function(){
												Ext.getBody().unmask();
												Ext.Msg.alert('提醒[Notice]',"数据检查表查完成!<BR>Table check finish!");
												},500);
												} else {
												Ext.Msg.alert('数据编辑',response.responseText);
											}
									 },
									 //提交失败的回调函数
									 failure:function(){
											Ext.Msg.alert('错误','数据提交错误！');
									 }
				});
				}
            },
            {
                xtype:"button",text:"分析表",iconCls:'d_fx',pressed:true,
                handler:function(){
				var record=store.getAt(rowIndex);
				var currid =record.data.id;
				Ext.Ajax.request({
				url : 'tablep.php',
				params : {op:'fx',id:currid},
				success:function(response){
											if (response.responseText=='ok') {
												Ext.getBody().mask('等待...','x-mask-loading');
                    							store.reload();
							                    setTimeout(function(){
												Ext.getBody().unmask();
												},500);
												Ext.Msg.alert('提醒[Notice]',"分析表完成!<BR>Analyzing the table!");
												} else {
												Ext.Msg.alert('数据编辑',response.responseText);
											}
									 },
									 //提交失败的回调函数
									 failure:function(){
											Ext.Msg.alert('错误','数据提交错误！');
									 }
				});
				}
            },
            {
                xtype:"button",text:"优化表",iconCls:'d_yh',pressed:true,
                handler:function(){
				var record=store.getAt(rowIndex);
				var currid =record.data.id;
				Ext.Ajax.request({
				url : 'tablep.php',
				params : {op:'yh',id:currid},
				success:function(response){
											if (response.responseText=='ok') {
												Ext.getBody().mask('等待...','x-mask-loading');
                    							store.reload();
							                    setTimeout(function(){
												Ext.getBody().unmask();
												},500);
												Ext.Msg.alert('提醒[Notice]',"优化表完成!<BR>Optimizing  the table!");
												} else {
												Ext.Msg.alert('数据编辑',response.responseText);
											}
									 },
									 //提交失败的回调函数
									 failure:function(){
											Ext.Msg.alert('错误','数据提交错误！');
									 }
				});
				}
            },
            {
                xtype:"button",text:"修复表",iconCls:'d_xf',pressed:true,
                handler:function(){
				var record=store.getAt(rowIndex);
				var currid =record.data.id;
				Ext.Ajax.request({
				url : 'tablep.php',
				params : {op:'xf',id:currid},
				success:function(response){
											if (response.responseText=='ok') {
												Ext.getBody().mask('等待...','x-mask-loading');
                    							store.reload();
							                    setTimeout(function(){
												Ext.getBody().unmask();
												},500);
												Ext.Msg.alert('提醒[Notice]',"修复表完成!<BR>Repair the table!");
												} else {
												Ext.Msg.alert('数据编辑',response.responseText);
											}
									 },
									 //提交失败的回调函数
									 failure:function(){
											Ext.Msg.alert('错误','数据提交错误！');
									 }
				});
				}
            }
       		]);
                menus.showAt(e.getXY());
            }
});
</script>
</head>
<body>
<div id="panel"></div>
</body>
</html>